package com.example.common.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 王鹤蒲
 * @Date 2023 10 12
 */
@Slf4j
public class ExcelUtils {

    public static void export(HttpServletResponse response,List<?> data, String fileName, Class<?> clazz,String sheetName){
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml,sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            fileName= URLEncoder.encode(fileName,"utf-8").replaceAll("\\+","%20");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("编码异常");
        }
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
        WriteCellStyle headWriteCellStyle=new WriteCellStyle();
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
        WriteFont headWriteFont=new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 10);
        headWriteCellStyle.setWriteFont(headWriteFont);
        headWriteCellStyle.setWrapped(true);
        WriteCellStyle contentWriteCellStyle=new WriteCellStyle();
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy=new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle);
        try {
            EasyExcel.write(response.getOutputStream(),clazz).autoCloseStream(Boolean.FALSE).excelType(ExcelTypeEnum.XLSX).registerWriteHandler(horizontalCellStyleStrategy).sheet(sheetName).doWrite(data);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }



    public static void  generateTemplate(HttpServletResponse response, String templateName, List<List<String>> headerList){
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml,sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            templateName= URLEncoder.encode(templateName,"utf-8").replaceAll("\\+","%20");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        response.setHeader("Content-disposition", "attachment;filename=" + templateName+"_"+System.currentTimeMillis()+".xlsx");
        response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
        WriteCellStyle headWriteCellStyle=new WriteCellStyle();
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
        WriteFont headWriteFont=new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 10);
        headWriteCellStyle.setWriteFont(headWriteFont);
        headWriteCellStyle.setWrapped(true);
        WriteCellStyle contentWriteCellStyle=new WriteCellStyle();
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy=new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle);
        try {
            EasyExcel.write(response.getOutputStream()).head(headerList).autoCloseStream(Boolean.FALSE).excelType(ExcelTypeEnum.XLSX).registerWriteHandler(horizontalCellStyleStrategy).sheet(templateName).doWrite(new ArrayList());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
